import org.eclipse.swt.graphics.Point;

import java.awt.Polygon;
import java.util.ArrayList;


public class ObjectDummy{
	
	private ArrayList<LineDummy> _llst = new ArrayList<LineDummy>();
	private ArrayList<Point> _ilist = new ArrayList<Point>();
	private ArrayList<Point> _plist = new ArrayList<Point>();
	private Anisotropy _ani = null;
	private ArrayList<Integer> _matchStory = new ArrayList<Integer>();
	private ArrayList<Integer> _matchStoryTmp = new ArrayList<Integer>();
	
	public ArrayList<Point> getShellPoints(){
		return _ilist;
	}
	
	
	public ArrayList<Integer> getMatchStory(){
		return _matchStory;
	}
	
	public ArrayList<Integer> getMatchStoryTmp(){
		return _matchStoryTmp;
	}
	
	public ArrayList<Point> getPointIntersectAni(){
		return _plist;
	}
	
	public void setPointList(ArrayList<Point> lss){
		_ilist = lss;
	}
	
	public void init(Anisotropy ani){
		_ani = ani;
	}

	public void move(int x, int y){
		
		for(Point p : _ilist){
			p.x = p.x + x;
			p.y = p.y + y;
		}
		for(Point p : _plist){
			p.x = p.x + x;
			p.y = p.y + y;
		}
		
	}
	
	public Polygon getPolygon(){
		int sz = _ilist.size();
		
		int x[] = new int[sz];
		int y[] = new int[sz];
		
		for(int i = 0; i < sz; i++){
			x[i] = _ilist.get(i).x;
			y[i] = _ilist.get(i).y;
		}
		
		return(new Polygon(x, y, sz));
		
	}
	
	
	public Point getMaxCoord(){
		if (_ilist.size() > 0){
			
			int x = _ilist.get(0).x;
			int y = _ilist.get(0).y;
			
			for(Point p : _ilist){
				if (p.x > x)
					x = p.x;
				if (p.y > y)
					y = p.y;
			}
			
			return new Point(x, y);
			
		}
		else
			return null;
	}
	
	
	public void normalize(){	
		if(_ilist.size() > 0){
			
			int x = _ilist.get(0).x;
			int y = _ilist.get(0).y;
			
			for(Point p : _ilist){
				if(p.x < x)
					x = p.x;
				if(p.y < y)
					y = p.y;
			}
			
			
			for(Point p : _ilist){
				p.x = p.x - x;
				p.y = p.y - y;
			}
			
			for(Point p : _plist){
				p.x = p.x - x;
				p.y = p.y - y;
			}
			
		}
	}
	
	
	public void lncalc(){
		for(int i = 0; i < _ilist.size(); i++){
			if ( i+1 >= _ilist.size()){
				
				LineDummy ln1 = new LineDummy(_ilist.get(i).x, _ilist.get(i).y, _ilist.get(0).x, _ilist.get(0).y);
				
				for(LineDummy ln2 : _ani.getLineList()){
					if(ln1.intersect(ln2)){
						Point p = ln1.intersectPoint(ln2);
						if (p != null){
							_plist.add(p);
							_matchStory.add(i);
						}
					}
					
				}
			}
			else {
				
				LineDummy ln1 = new LineDummy(_ilist.get(i).x, _ilist.get(i).y, _ilist.get(i+1).x, _ilist.get(i+1).y);
				
				for(LineDummy ln2 : _ani.getLineList()){
					if(ln1.intersect(ln2)){
						Point p = ln1.intersectPoint(ln2);
						if (p != null){
							_plist.add(p);
							_matchStory.add(i);
						}
					}
				}
				
			}
		}
		
		//-----------------
		System.out.println("================");
		for(Integer p : _matchStory)
			System.out.println(p);
		System.out.println("================");
		//-----------------
		
	}
	
	
	public ArrayList<Point> intersectPoints(){
		
		ArrayList<Point> nplist = new ArrayList<Point>();
		
		for(int i = 0; i < _ilist.size(); i++){
			if ( i+1 >= _ilist.size()){
				
				LineDummy ln1 = new LineDummy(_ilist.get(i).x, _ilist.get(i).y, _ilist.get(0).x, _ilist.get(0).y);
				
				for(LineDummy ln2 : _ani.getLineList()){
					if(ln1.intersect(ln2)){
						Point p = ln1.intersectPoint(ln2);
						if (p != null){
							nplist.add(p);
							_matchStoryTmp.add(i);
						}
					}
					
				}
			}
			else {
				
				LineDummy ln1 = new LineDummy(_ilist.get(i).x, _ilist.get(i).y, _ilist.get(i+1).x, _ilist.get(i+1).y);
				
				for(LineDummy ln2 : _ani.getLineList()){
					if(ln1.intersect(ln2)){
						Point p = ln1.intersectPoint(ln2);
						if (p != null){
							nplist.add(p);
							_matchStoryTmp.add(i);
						}
					}
				}
				
			}
		}
		

		
		return nplist;
		
	}
	
	
	public boolean match(){
		
		if(intersectPoints().size() == getPointIntersectAni().size()){
			return true;
		}
											
		if (_matchStory.size() == _matchStoryTmp.size())
			return true;
			
		for(int i = 0; i < _matchStory.size(); i++){
			if (_matchStory.get(i) != _matchStoryTmp.get(i)){
				return false;
			}
		}
		
		
		//-----------------
//		System.out.println("----------------");
//		for(Integer p : _matchStoryTmp)
//			System.out.println(p);
//		System.out.println("----------------");
		//-----------------
		
		return true;
	}
	

}
